<template>
	<div>
		<div class="h2">问卷问题列表</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="名称">
							<a-input autocomplete="off" class="field-right" placeholder="请输入名称搜索"
								v-decorator="[`key`]" />
						</a-form-item>
					</a-col>
					<!-- <a-col :span="8">
						<a-form-item label="部门">
							<a-select :allowClear="true" v-decorator="['departCode']" placeholder="全部" optionFilterProp="children" showSearch>
								<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col> -->
					<a-col :span="8" style="margin-top:4px;">
						<a-button-group size="default" class="option">
							<a-button type="primary" @click="handleSearch">搜索</a-button>
							<a-button @click="handleReset">重置</a-button>
							<a-button type="primary" @click="addList">新增</a-button>
						</a-button-group>
					</a-col>
				</a-row>
			</a-form>
		</div>
		
		<div>
			<el-table :data="data" v-loading="loading" :max-height="tableHeight" row-key="ID" :expand-row-keys="expends" highlight-current-row
				style="width: 100%;" :row-class-name="getRowClass">
				<el-table-column type="expand" prop="children" >
					<template scope="scope">
						<el-table :data="scope.row.items" v-if="scope.row.items.length>0">
							<!-- <el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column> -->
							<el-table-column prop="Name" label="选项"   width="240"></el-table-column>
							<el-table-column prop="Score" label="分值"  show-overflow-tooltip>
							</el-table-column>
							<el-table-column prop="Score" label="是否是正确答案"  show-overflow-tooltip>
								<template slot-scope="scope1">
									<a-tag :color="scope1.row.IsCorrectAnswer?'green':'red'">{{ scope1.row.IsCorrectAnswer?'是':'否' }}
									</a-tag>
								</template>
							</el-table-column>
							<el-table-column prop="IsEnable" label="操作"  width="180" fixed="right">
								<template slot-scope="scope1">
									<i title="修改" class="iconfont icon-editor1" style="color: #1890FF;"
										@click="open2(scope1.row, 'modify')"></i>
										<a-tooltip placement="top" v-if="scope1.$index!= 0" >
											<template slot="title">升序</template>
											<i title="升序" class="iconfont icon-paixu1" style="color: green;" @click="sortList(scope1.row,1)"></i>
										</a-tooltip>
										<a-tooltip placement="top" v-if="scope1.$index != scope.row.items.length - 1">
											<template slot="title">降序</template>
											<i title="降序" class="iconfont icon-paixu" style="color: brown;" @click="sortList(scope1.row,2)"></i>
										</a-tooltip>
									<i title="删除" class="iconfont icon-delete" style="color: red;"
										@click="deletemember(scope1.row)"></i>
								</template>
							</el-table-column>
						</el-table>
						<div v-else style="text-align: center;">暂无数据</div>
					</template>
					
				</el-table-column>
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="Title" label="名称"  width="200" show-overflow-tooltip></el-table-column>
				<el-table-column prop="IsEnable" label="类型">
					<template slot-scope="scope">
						<a-tag color="green">{{ scope.row.Type==1 ? '单选' :scope.row.Type==2 ? '多选' :scope.row.Type==3 ? '填空' :'' }}
						</a-tag>
					</template>
				</el-table-column>
				<el-table-column prop="CorrectAnswer" label="正确答案"  width="200" show-overflow-tooltip>
					<template slot-scope="scope">
					<span v-if="scope.row.Type==3">{{scope.row.CorrectAnswer}}</span>
					<span v-else></span>
					</template>
				</el-table-column>
				
				

				<el-table-column label="操作" width="180" fixed="right">
					<template slot-scope="scope">
						<oprateBtn title="修改" icon="icon-editor1" color="#1890FF" @tap="modifyList(scope.row,'modify')">
						</oprateBtn>
						<i title="新增选项" class="iconfont icon-shezhi" v-if="scope.row.Type!=3" @click="open(scope.row)"
							style="color: #1890FF;"></i>
							<a-tooltip placement="top" v-if="scope.$index!= 0" >
								<template slot="title">升序</template>
								<i title="升序" class="iconfont icon-paixu1" style="color: green;" @click="sortList2(scope.row,1)"></i>
							</a-tooltip>
							<a-tooltip placement="top" v-if="scope.$index != data.length - 1">
								<template slot="title">降序</template>
								<i title="降序" class="iconfont icon-paixu" style="color: brown;" @click="sortList2(scope.row,2)"></i>
							</a-tooltip>
						<oprateBtn title="删除" icon="icon-delete" color="red" @tap="deleteList(scope.row,1)"></oprateBtn>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="pagination.currentPage" :page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"></el-pagination>
		</div>
		<add :text="text" :DepartCodeList="DepartCodeList" :ClassList="ClassList" v-show="addIf" @closeFun="closeFun">
		</add>
		<addItem :text="textItem" v-show="addIfItem" @closeFun="closeFun">
		</addItem>
	</div>
</template>

<script>
	import Cache from '@/utils/cache';
	import add from './add';
	import addItem from './addItem';
	import Func from '@/utils/func';
	import moment from 'moment';
	import {
		ManageEvaluationSubjectDoGetPageList,
		ManageEvaluationSubjectDoDelete,
		ManageEvaluationSubjectItemDoDelete,
		ManageEvaluationSubjectItemDoSort,
		GetDepartList,
		GetClassList,
		DoUnbundTeacher,
		DoEnableTeacher,
		ManageEvaluationSubjectDoSort
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add,
			addItem
		},
		data() {
			return {
				data,
				expends:[],
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				addIfItem: false,
				text: {},
				textItem: {},
				visible: false,
				confirmLoading: false,
				itemId: '',
				gradeYear: null,
				yearPickShow: false,
				departmentList: [],
				worklist: [],
				loading: false,
				values: {},
				pages: {
					pageIndex: 1,
					pageSize: 20
				},
				tableHeight: parseFloat(window.innerHeight - 160),
				ClassList: [],
				DepartCodeList: [],
				requestUrl: this.global.requestUrl,
				token: Cache.get('token')
			};
		},
		computed: {},
		created() {
			this.getList();
			// this.getOtherList();
		},
		mounted() {},
		methods: {
			moment,
			getRowClass(row,rowIndex){
			    if(row.row.items.length==0){  //判断当前行是否有子数据或者根据实际情况设置
			          return 'row-expand-cover'
			  }
			},
			exportList() {
				this.form.validateFields((error, values) => {
					let data = {};
					data.key = values.key;
					data.departCode = values.departCode;
					let hrefUrl = this.requestUrl + 'Manage/Teacher/DoExport?userToken=' + encodeURIComponent(this
						.token);
					let hrefStr = '';
					for (let key in data) {
						if (data[key]) {
							hrefStr += key + '=' + data[key] + '&';
						}
					}
					window.open(hrefUrl + '&' + hrefStr, '_blank');
				});
			},
			handleOpenChange(status) {
				this.yearPickShow = status;
			},
			handlePanelChange(value, mode) {
				this.gradeYear = moment(value).format('YYYY');
				this.yearPickShow = false;
			},
			yearChange(date, dateString) {
				if (!date) {
					this.gradeYear = null;
				}
			},
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.pagination.currentPage = 1;
					this.values = values;
					if (this.gradeYear) {
						this.values.grade = this.gradeYear;
					}
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.values = {};
				this.gradeYear = null;
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				if (this.values) {
					data = this.values;
				}
				data.evaluationID = this.$route.query.ID;
				data.pageIndex = this.pagination.currentPage;
				data.pageSize = this.pagination.pageSize;
				let res = await ManageEvaluationSubjectDoGetPageList(data);
				this.loading = false;
				this.data = res.data.data;
				const pagination = {
					...this.pagination
				};
				pagination.total = res.data.pageInfo.TotalItemCount;
				this.pagination = pagination;
			},
			async getOtherList() {
				let res = await GetClassList();
				this.ClassList = res.data.data;
				let res1 = await GetDepartList();
				this.DepartCodeList = res1.data.data;
			},
			addList() {
				this.text = {};
				this.addIf = !this.addIf;
			},
			open(text) {
				this.textItem = {
					EvaluationSubjectID: text.ID,
					time: new Date(),
					type:'add'
				};
				this.addIfItem = !this.addIfItem;

			},
			open2(text){
				// console.log(text);
				text = Object.assign(text, {
					time: new Date(),
					type:'edit'
				});
				this.addIfItem = !this.addIfItem;
				this.textItem = text;
			},
			modifyList(text) {
				text = Object.assign(text, {
					time: new Date()
				});
				this.addIf = !this.addIf;
				this.text = text;
			},
			deletemember(text){
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定删除该选项吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = await ManageEvaluationSubjectItemDoDelete(data);
						console.log(res);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			deleteList(text, type) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: type == 1 ? `您确定删除该问题吗？` : type == 2 ? `您确定要解绑该教师的微信吗？` : `您确定要修改该老师的状态吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = type == 1 ? await ManageEvaluationSubjectDoDelete(data) : type ==
							2 ?
							await DoUnbundTeacher(data) : await DoEnableTeacher(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			seeList(text, name) {
				this.$router.push({
					name: name,
					query: {
						ID: text.ID,
						Name: text.Name,
						UserNo: text.UserNo
					}
				});
			},
			closeFun() {
				this.addIf = false;
				this.addIfItem = false;
				this.getList();
			},
			async sortList(text,type){
				// 1=向上 2=向下
				let data={}
				data.id = text.ID
				data.type = type
				let res = await ManageEvaluationSubjectItemDoSort(data);
				if(res.data.code==0){
					this.getList();
					this.expands = [];
				  this.expands.push(text.ID);
					this.$message.success(res.data.msg)
				}else{
					this.$message.error(res.data.msg)
				}
			},
			async sortList2(text,type){
				// 1=向上 2=向下
				let data={}
				data.id = text.ID
				data.type = type
				let res = await ManageEvaluationSubjectDoSort(data);
				if(res.data.code==0){
					this.getList();
					this.$message.success(res.data.msg)
				}else{
					this.$message.error(res.data.msg)
				}
			},
		}
	};
</script>

<style lang="less" scoped>
	/deep/ .el-table .row-expand-cover .cell .el-table__expand-icon {
	  display: none;
	}
	/deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	/deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.ant-calendar-picker {
		width: 100%;
	}
</style>
